﻿using Permission.Controllers.Filter;
using Permission.DataModel;
using Permission.Service;
using Permission.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Permission.Controllers.ManageArea
{
    public class HomeController : Controller
    {
        private readonly IUserService _userService;
        private readonly IRoleService _roleService;

        public HomeController(IUserService userService, IRoleService roleService)
        {
            _userService = userService;
            _roleService = roleService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult UserList()
        {
            return Json(_userService.Users(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult CreateOrEditUser(string id)
        {
            ViewBag.roles = _roleService.GetListRoleIdName();

            if (!string.IsNullOrEmpty(id))
                ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(_userService.LoadUser(id));
            else
                ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(new Sys_User { });
            return View();
        }

        public PartialViewResult EditUser(string id)
        {
            ViewBag.roles = _roleService.GetListRoleIdName();

            if (!string.IsNullOrEmpty(id))
                ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(_userService.LoadUser(id));
            else
                ViewBag.user = Newtonsoft.Json.JsonConvert.SerializeObject(new Sys_User { });
            
            return PartialView("_CreateOrEditUser");
        }

        [HttpPost]
        public JsonResult SaveUser(UserModel user)
        {
            var result = _userService.SaveUser(user);
            return Json(result);
        }

        [HttpPost]
        public JsonResult DelUser(string id)
        {
            var result = _userService.DelUser(id);
            return Json(result);
        }

        public ActionResult Role()
        {
            return View();
        }

        [HttpGet]
        public JsonResult RoleList()
        {
            return Json(_roleService.Roles(), JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult CreateOrEditRole(string id)
        {
            if (!string.IsNullOrEmpty(id))
                ViewBag.role = Newtonsoft.Json.JsonConvert.SerializeObject(_roleService.LoadRole(id));
            else
                ViewBag.role = Newtonsoft.Json.JsonConvert.SerializeObject(new Sys_Role { });
            return PartialView("_CreateOrEditRole");
        }

        [HttpPost]
        public JsonResult CreateOrEditRole(Sys_Role model)
        {
            var result = _roleService.SaveRole(model);
            return Json(result);
        }

        /// <summary>
        /// 过滤器
        /// </summary>
        /// <returns></returns>
        [Operate]
        public ActionResult FilterResult()
        {
            return View();
        }
    }
}
